/* 
	Started by granQ.sqf
	Sends the position that supposed to be attacked and which side the ai is.
	This since earlier versions had support for AI on both sides.
*/

_pos = _this select 0;
_aiSide = _this select 1;


if ((_aiSide == side0 AND !Side0AssBought) OR  (_aiSide == side1 AND !Side1AssBought)) then
{

	if (_aiSide == side0)  then { _money = side0_money;_ic = side0_ic;_lc = side0_lc; _options = side0CommanderMenu;};
	if (_aiSide == side1)  then { _money = side1_money;_ic = side1_ic;_lc = side1_lc; _options = side1CommanderMenu;};
	_bought = false;

	/* 
		Find out nearest base to deploy from.
		Adjust the positon so the AI doesn't move directly towards the flag. Needs to be more sneaky.
	*/

	_base = [_pos, _aiSide] call fGetNearestFOB;

	_x = random 500 - random 500;
	_y = random 500 - random 500;
	_pos = [(getpos _pos select 0) + _x, (getpos _pos select 1) +_y,0];




	/*
		Decide what the AI will send
	*/


	if ((_aiSide == side0) AND (side0_ic > 10))  then { side0_ic = side0_ic - 5; Side0SpecialIC = Side0SpecialIC + 5; _SpecialIC = Side0SpecialIC; };
	if ((_aiSide == side1) AND (side0_ic > 10))  then { side1_ic = side1_ic - 5; Side1SpecialIC = Side1SpecialIC + 5; _SpecialIC = Side1SpecialIC; };

	if ((_money > 300) AND (_ic > 1500) AND (_lc > 300) AND !(PreferAir) AND (random 7 > 4)) then { _bought =  true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_CASplane.sqs"; _xmoney = 300; _xic = 1500; _xLC = 300;};
	if ((_money > 300) AND (_ic > 1500) AND (_lc > 300) AND !(PreferAir)) then { _bought =  true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_MBT.sqs"; _xmoney = 300; _xic = 1500; _xLC = 300;};

	if ((_money > 400) AND (_ic > 400) AND (_lc > 90) AND (PreferAir)) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_AA.sqs"; _xmoney = 50; _xic = 400; _xLC = 90;};
	if ((_money > 300) AND (_ic > 400) AND (_lc > 40) AND !(PreferTanks)) then { _bought =  true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_mech.sqs"; _xmoney = 100; _xic = 300; _xLC = 20;};
	if ((_money > 200) AND (_ic > 200) AND (_lc > 90) AND !(PreferAir) AND (PreferTanks)) then { _bought =  true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_AT.sqs"; _xmoney = 50; _xic = 200; _xLC = 90;};
	if ((_money > 200) AND (_ic > 100) AND (_lc > 70)  AND !(PreferTanks)) then { _bought = true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_frt.sqs"; _xmoney = 200; _xic = 30; _xLC = 70; BuiltAssault = false;};

	if ((_aiSide == side0) AND (_money > 300) AND (_SpecialIC > 1500) AND (_lc > 300) AND !(PreferAir)) then { _bought =  true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_MBT.sqs"; _xmoney = 300; _xic = 0; _xLC = 300; Side0SpecialIC = Side0SpecialIC - 1500;};
	if ((_aiSide == side1) AND (_money > 300) AND (_SpecialIC > 1500) AND (_lc > 300) AND !(PreferAir)) then { _bought =  true; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_MBT.sqs"; _xmoney = 300; _xic = 0; _xLC = 300; Side1SpecialIC = Side1SpecialIC - 1500;};


	if ((_aiSide == side0) AND (_money > 900) AND (_ic > 10) AND (_lc > 20) AND (PreferTanks)) then { _bought = "Aircav"; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_aircav.sqs"; _xmoney = 900; _xic = 10; _xLC = 20;};
	if ((_aiSide == side1) AND (_money > 900) AND (_ic > 10) AND (_lc > 20) AND (PreferTanks)) then { _bought = "Paradrops"; [_pos,_harbour,_aiSide] exec "server\coop\bought\commanderOrdered_AirPlanePara.sqs"; _xmoney = 900; _xic = 10; _xLC = 20;};


	if (_bought) then
	{
			/*
				Make the AI pay and wait a bit so he doesn't spam units.
			*/
		if (_aiSide == side0) then { Side0AssBought = true;};
		if (_aiSide == side1) then { Side1AssBought = true;};
		if (_aiSide == side0) then { side0_money = side0_money - _xmoney; side0_ic = side0_ic - _xic; side0_lc = side0_lc - _xLC; };
		if (_aiSide == side1) then { side1_money = side1_money - _xmoney; side1_ic = side1_ic - _xic; side1_lc = side1_lc - _xLC; };
		sleep (random 350);

		if (_aiSide == side0) then { Side0AssBought = false;};
		if (_aiSide == side1) then { Side1AssBought = false;};
	};
};
